<?php
/**
 * 
 * This class manages the roles in this application<br>
 * Roles are Administrator or Operator
 * 
 * @author Dennis
 */
class RolesController extends AppController{

	var $uses = array('Role','State');

	/**
	 * (non-PHPdoc)
	 * @see Controller::beforeFilter()
	 */
	function beforeFilter(){
		parent::beforeFilter();
	}

	/**
	 * (non-PHPdoc)
	 * @see AppController::index()
	 */
	public function index(){
		$this->data = $this->Role->find('all');
		if(empty($this->data)){
			$message = 'Es existieren keine Rollen';
			parent::doFlash(array('Info'=>$message));
		}
	}


	/**
	 * Adds a role
	 */
	function add(){
		if(!empty($this->data)){
			$this->data['Role']['state_id'] = 1;
			$ok = $this->Role->save($this->data);
			$roleName = '"'.$this->data['Role']['role_name'].'"';
			if($ok){
				$message = sprintf($this->translate('roles/saved'),$roleName);
				parent::doFlash(array('Info' => $message));
				$this->redirect(array('action'=>'index'));
			}else{
				$message =  sprintf($this->translate('roles/notSaved'),$roleName);
				parent::doFlash(array('Fehler' => $message));
			}
		}
	}


	/**
	 * Update a roles data
	 *
	 * @param int $id - id for role to update
	 */
	function update($id=null){

		// set defaults
		$messages = null;

		// check if the given role id is a valid id
		if(!is_numeric($id)){
			$message = sprintf($this->translate('roles/invalidId'),$id);
			parent::doFlash(array('Fehler'=>$messages));
			$this->redirect(array('action'=>'index'));
			return;
		}

		// check if anybody wants to change role of administrator
		if($id == 1){
			$message = sprintf($this->translate('roles/notChangeable'),$roleName);
			parent::doFlash(array('Fehler' => $message));
			$this->redirect(array('action'=>'index'));
			return;
		}

		// check if requested role exists
		$role = $this->Role->findById($id);
		if(empty($role)){
			$message = sprintf($this->translate('roles/notExist'),$id);
			parent::doFlash(array('Fehler' => $message));
			$this->redirect(array('action'=>'index'));
			return;
		}

		// check request data and update role
		if(!empty($this->data)){
			$this->Role->id = $id;
			$role = $this->Role->findById($id);
			$ok = $this->Role->save($this->data);

			$name = $this->data['Role']['role_name'];
			if(strlen($name)==0){
				$name = $role['Role']['role_name'];
			}
			if($ok){
				$message = sprintf($this->translate('roles/changed'),$name);
				parent::doFlash(array('Info' => $message));
				$this->redirect(array('action'=>'index'));
			}else{
				$message = sprintf($this->translate('roles/notChanged'),$name);
				parent::doFlash(array('Fehler' => $message));
			}

		}

		$this->data = $this->Role->findById($id);

		// get the states list ---------------
		$params = array (
			'fields' => array('State.id','State.state_name'),
		);
		$states = $this->Role->State->find('list',$params);
		$this->set('states',$states);

	}

	/**
	 * Set a new state for a given role
	 *
	 * @param int $roleId - id for the role to set new state for
	 * @param int $stateId - new state id for given role
	 */
	public function setState($roleId=null, $stateId=null){

		// set defaults
		$messages = null;
		$allOk = true;
		$role = null;
		$state = null;

		// check if requested role exists
		if(is_numeric($roleId)){
			$this->Role->id = $roleId;
			$role = $this->Role->read();
			if(empty($role)){
				$messages['Fehler'][] = sprintf($this->translate('roles/notExist'),$roleId);
				$allOk = false;
			}
		}else{
			$messages['Fehler'][] = sprintf($this->translate('roles/invalidId'),$roleId);
			$allOk = false;
		}

		// check if requested state exists
		if(is_numeric($stateId)){
			$this->State->id = $stateId;
			$state = $this->State->read();
			if(empty($state)){
				$messages['Fehler'][] = sprintf($this->translate('states/notExist'),$stateId);
				$allOk = false;
			}
		}else{
			$messages['Fehler'][] = sprintf($this->translate('states/invalidId'),$stateId);
			$allOk = false;
		}

		// save changes when state and role are existing and valid
		if($allOk){
			$this->Role->set('state_id',$stateId);
			$ok = $this->Role->save($this->data);
			$roleName = $role['Role']['role_name'];
			$stateName = $state['State']['state_name'];
			if($ok){
				if($state['State']['id']==1){
					$messages['Info'][] = sprintf($this->translate('roles/activated'),$roleName);
				}else{
					$messages['Info'][] = sprintf($this->translate('roles/deactivated'),$roleName);
				}
			}else{
				if($state['State']['id']==1){
					$messages['Info'][] = sprintf($this->translate('roles/notActivated'),$roleName);
				}else{
					$messages['Info'][] = sprintf($this->translate('roles/notDeaktivated'),$roleName);
				}
			}
		}

		parent::doFlash($messages);
		$this->redirect(array('action'=>'index'));
	}

	/**
	 * Deletes a role

	 * @param int $roleId - id for role which should be deleted
	 */
	function delete($roleId=null){

		// set defaults
		$messages = null;

		// check if given role id is a valid id
		if(is_numeric($roleId)){
			$role = $this->Role->findById($roleId);
			$dependencies = array();

			// check methods
			if(!empty($role['Method'])){
				$dependencies[] = sizeof($result['Method']).' Method(en)';
			}

			//check user
			if(!empty($role['User'])){
				$dependencies[] = sizeof($result['User']).' User';
			}

			$ok = sizeof($dependencies)==0;
			if($ok){
				$this->Role->delete($roleId);
				$messages['Info'][]= sprintf($this->translate('roles/deleted'),$role['Role']['role_name']);
			}else{
				$deps .= implode(', ', $dependencies);
				$messages['Fehler'][]= sprintf($this->translate('roles/hasDependencies'),$role['Role']['role_name'],$deps);
			}
			$this->redirect(array('action'=>'index'));
		}else{
			$messages['Fehler'][]= sprintf($this->translate('roles/invalidId'),$roleId);
		}

		parent::doFlash($messages);
	}

}
?>
